package GUI.Lesson;

import java.awt.*;
import java.awt.event.*;

public class TextCalculatorBetter {
    public static void main(String[] args) {
        new Calculators().loadFrame();
    }
}


//优化后的面向对象
//计算机类
class Calculators extends Frame {
    TextField textField1,textField2,textField3;

    public void loadFrame() {
        //三个文本框
         textField1 = new TextField(10);
         textField2 = new TextField(10);
         textField3 = new TextField(20);

        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new Actions(this));

        //一个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());

        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        pack();
        setVisible(true);
    }
}

//监听类
class Actions implements ActionListener {
    Calculators calculators = null;
    public Actions(Calculators calculators) {
        this.calculators = calculators;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1 = Integer.parseInt(calculators.textField1.getText());
        int n2 = Integer.parseInt(calculators.textField2.getText());

        //2.将这个值加法运算后赋值给第三个文本框
        calculators.textField3.setText(""+n1+n2);

        //清楚前两个文本框
        calculators.textField1.setText("");
        calculators.textField2.setText("");
    }
}
